﻿@model BeYourMarket.Web.Models.ListingModel

@{
    ViewBag.Title = "[[[Dashboard]]]";
}

<div class="body-content container-fluid">
    <div class="main-section row">
        <div class="container-fluid mrg-tb">

            @Html.Partial("_DashBoardSide", "dashboard")

            <div class=" col-md-9">
                <div id="myTabContent" class="tab-content ">
                    <div role="tabpanel" class="tab-pane card-content fade active in" id="listings" aria-labelledby="listings-tab">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                [[[My Listings]]]
                            </div>
                            <div class="panel-body">
                                <div class="pull-left">
                                    <form class="form-inline" action="@Url.Action("Dashboard", "Manage")" method="get" role="form">
                                        <input type="text" id="searchText" name="searchText" class="input-sm form-control" placeholder="[[[Search your listings...]]]" value="@Request.QueryString["searchText"]">
                                        <button class="btn btn-primary btn-sm" type="submit"><i class="fa fa-search"></i> [[[Search]]]</button>
                                    </form>
                                </div>
                                <div class="pull-right">
                                    <a class="btn btn-default btn-sm hidden-sm hidden-xs" href="@Url.Action("ListingUpdate", "Listing")"><i class="mdi-content-add-circle-outline"></i> [[[Publish new listing]]]</a>
                                </div>
                            </div>

                            <ul class="media-list list-group">
                                @foreach (var item in Model.Listings)
                                {
                                    <li id="item_@item.ListingCurrent.ID" class="media list-group-item">
                                        <a class="pull-left col-xs-2" href="@Url.Action("Listing", "Listing", new { id = item.ListingCurrent.ID })">
                                            <img class="item-thumbnail img-responsive img-rounded" alt="Thumbnail" src="@item.UrlPicture">
                                        </a>
                                        <div class="col-xs-6">
                                            <h4 class="media-heading"><a href="@Url.Action("Listing", "Listing", new { id = item.ListingCurrent.ID })"> @item.ListingCurrent.Title</a></h4>
                                            <p class="hidden-xs">
                                                @item.ListingCurrent.Description.PrefixString(150)
                                            </p>
                                        </div>
                                        <div class="col-xs-4">
                                            <div class="pull-right">
                                                <a class="btn btn-primary btn-sm" href="@Url.Action("ListingUpdate", "Listing", new { id = item.ListingCurrent.ID })"><i class="fa fa-edit"></i><span class="hidden-xs">[[[Edit]]]</span></a>
                                                <a class="btn btn-default btn-sm" href="javascript:void(0);" onclick="confirmDelete(@item.ListingCurrent.ID, '@item.ListingCurrent.Title');"><i class="fa fa-remove"></i><span class="hidden-xs">[[[Delete]]]</span></a>
                                            </div>
                                        </div>
                                    </li>
                                }
                            </ul>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script type="text/javascript">
        function confirmDelete(id, title) {
            swal({
                title: "[[[Are you sure?]]]",
                text: "[[[You will delete]]] " + title,
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "[[[Yes, delete it!]]]",
                cancelButtonText: "[[[No, cancel please!]]]",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function (isConfirm) {
                if (isConfirm) {
                    $.post('@Url.Action("ListingDelete","Listing")', { id: id })
                    .done(function (result) {
                        if (result.Success) {
                            $("#item_" + id).remove();
                            swal("[[[Deleted!]]]", result.Message, "success");
                        } else {
                            swal("[[[Failed!]]]", result.Message, "error");
                        }
                    })
                    .fail(function (xhr, textStatus, errorThrown) {
                        swal("[[[Failed!]]]", xhr.responseText, "error");
                    })
                } else {
                    swal("[[[Cancelled!]]]", "", "error");
                }
            });
        }
    </script>
}